<?php

namespace app\shopping\controller;

use think\Controller;

use app\common\library\Email;
/**
 * 用户接口
 */

class Business extends Controller
{
    public function __construct(){
        parent::__construct();
        $this->BusinessModel = model('Business.User');
    }

    /**
     * 用户注册
     *
     * @ApiTitle    (用户注册)
     * @ApiSummary  (用户注册)
     * @ApiMethod   (POST)
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function register(){
        // 判断是否是post请求
        if($this->request->isPost()){
            // 接收手机号和密码
            $mobile = $this->request->param('mobile','','trim');
            $password = $this->request->param('password','','trim');
            // var_dump($mobile,$password);
            // exit;

            // 判断密码是否为空
            if(empty($password)){
                $this->error("密码不能为空");
                exit;
            } 

            // 获取密码盐
            $salt = randstr();
            // var_dump($salt);
            // exit;
            // 密码加密
            $password = md5($password.$salt);

            $data = [
                'mobile' => $mobile,
                'nickname' => $mobile,
                'password' => $password,
                'salt' => $salt,
                'gender' => '0', //性别
                'deal' => '0', //成交状态
                'money' => '0',//余额
                'auth' => '0' //实名认证
            ];

             // 查询一个字段
             $data['sourceid'] = model('common/Business/Source')->where(['name'=>['LIKE',"%家居商城%"]])->value('id');

             // var_dump($sourceid);
             // exit;

            // var_dump($data);
            // exit;

            // 执行插入，返回自增的条数
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data); 
            
            if($result === FALSE){
                // 失败
                $this->error($this->BusinessModel->getError());
                exit;
            }else{
                $this->success('注册成功','/business/login');
                exit;
            }
        }
    }


    /**
     * 用户登录
     *
     * @ApiTitle    (用户登录)
     * @ApiSummary  (用户登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function login(){
        // 判断是否为post请求
        if($this->request->isPost()){
            $mobile = $this->request->param('mobile','','trim');
            $password = $this->request->param('password','','trim');
            // var_dump($mobile,$password);
            // exit;

            $user =$this->BusinessModel->where(['mobile'=>$mobile])->find();
            // var_dump($source->toArray());
            // exit;
            if(!$user){
                $this->error("手机号未注册，请注册后登录");
            }

            $salt = $user->salt;
            if(md5($password.$salt) != $user['password']){
                $this->error("密码错误，登录失败");
            }
            // 删除
            unset($user['password']);
            unset($user['salt']);

            // 跳转会员中心
            $this->success("登录成功",'/business/index',$user);
            exit;
            
        }
    }

    /**
     * 判断用户是否登录
     *
     * @ApiTitle    (判断用户是否登录)
     * @ApiSummary  (判断用户是否登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="int", required=true, description="ID")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function check(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $mobile = $this->request->param('mobile','','trim');

            // 查询是否存在
            $where = ['id' => $id,'mobile' => $mobile];
            $business = $this->BusinessModel->where($where)->find();

            if($business){
                unset($business['password']);
                unset($business['salt']);

                $this->success("用户验证成功",null,$business);
                exit;
                
            }else{
                $this->error('用户不存在');
                exit;
            }
        }
    }


    /**
     * 修改客户基本资料
     *
     * @ApiTitle    (修改客户基本资料)
     * @ApiSummary  (修改客户基本资料)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="int", required=true, description="ID")
     * @ApiParams   (name="nickname", type="string", required=true, description="昵称")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="password", type="string", required=false, description="密码")
     * @ApiParams   (name="email", type="string", required=true, description="邮箱")
     * @ApiParams   (name="gender", type="enum", required=true, description="性别")
     * @ApiParams   (name="region", type="string", required=true, description="所在地区")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function profile(){
        //判断是否有Post过来数据
        if($this->request->isPost())
        {
            //可以一次性接收到全部数据
            $id = $this->request->param('id', 0, 'trim');
            $nickname = $this->request->param('nickname', '', 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');
            $email = $this->request->param('email', '', 'trim');
            $gender = $this->request->param('gender', '0', 'trim');
            $code = $this->request->param('code', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            //判断用户是否存在
            $business = $this->BusinessModel->find($id);

            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }

            // 直接组装数据
            $data = [
                'id' => $business['id'], //因为我们要执行更新语句
                'nickname' => $nickname,
                'mobile' => $mobile,
                'gender' => $gender,
            ];

            //如果密码不为空 修改密码
            if(!empty($password))
            {
                //重新生成一份密码盐
                $salt = randstr();

                $data['salt'] = $salt;
                $data['password'] = md5($password.$salt);
            }
            
            //判断是否修改了邮箱 输入的邮箱 不等于 数据库存入的邮箱
            //如果邮箱改变，需要重新认证
            if($email != $business['email'])
            {
                $data['email'] = $email;
                $data['auth'] = '0';
            }

            //判断是否有地区数据
            if(!empty($code))
            {
                //查询省市区的地区码出来
                $parent = model('Region')->where(['code' => $code])->value('parentpath');
                
                if(!empty($parent))
                {
                    $arr = explode(',', $parent);
                    $data['province'] = isset($arr[0]) ? $arr[0] : null;
                    $data['city'] = isset($arr[1]) ? $arr[1] : null;
                    $data['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }

            //判断是否有图片上传
            if(isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0)
            {
                $success = build_upload('avatar');

                //如果上传失败，就提醒
                if(!$success['result'])
                {
                    $this->error($success['msg']);
                    exit;
                }

                //如果上传成功
                $data['avatar'] = $success['data'];
            }
            
            //执行更新语句 数据验证 -> 需要用到验证器

            //这是插入语句
            // $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            //更新语句 如果是更新语句，需要给data提供一个主键id的值 这就是更新语句 使用验证器的场景
            $result = $this->BusinessModel->validate('common/Business/Business.profile')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->BusinessModel->getError());
                exit;
            }

            //判断是否有旧图片，如果有就删除
            if(isset($data['avatar']))
            {
                is_file(".".$business['avatar']) && @unlink(".".$business['avatar']);
            }

            $this->success('更新资料成功');
            exit;
        }
    }


    /**
     * 邮箱验证
     *
     * @ApiTitle    (邮箱验证)
     * @ApiSummary  (邮箱验证)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="int", required=true, description="ID")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function email(){

        if($this->request->isPost()){
            // 加载模型
            $EmsModel = model('common/Ems');
            $id = $this->request->param('id',0,'trim');
            $business = $this->BusinessModel->find($id);
            if(!$business){
                $this->error('用户不存在');
                exit;
            }

            $email = empty($business['email']) ? '' : $business['email'];
            if(empty($email)){
                $this->error('邮箱地址为空');
                exit;
            }
            $action = $this->request->param('action','','trim');

            if($action == 'send'){
                // 生成一个验证码
                $code = randstr(5);
                
                // 开启事务
                $EmsModel->startTrans();

                // 删除掉之前旧的验证码
                $EmsModel->where(['email'=> $email])->delete();

                // 把验证码插入到数据库表中
                $data = [
                    'event' => 'auth',
                    'email' => $email,
                    'code' => $code,
                    'times' => 0
                ];

                // 插入数据
                $ems = $EmsModel->save($data);

                if($ems === FALSE){
                    $this->error('验证码插入失败');
                    exit;
                }
                // 邮件主题
                $name = config('site.name');
                $subject = "【{$name}】邮箱验证";

                // 组装文字信息
                $message = "<div>感谢您的使用，您的邮箱验证码为：<b>$code</b></div>";

                // 实例化邮箱验证类
                $PhpMailer = new Email;

                // 邮箱发送有规律，不可以发送关键词
                $result = $PhpMailer
                    ->to($email)
                    ->subject($subject)
                    ->message($message)
                    ->send();

                // 检测邮箱发送成功还是失败
                if($result){
                    // 发送验证码成功
                    // 将事务提交，提交的意思是让刚插入的记录真实存在到数据表中
                    $EmsModel->commit();
                    $this->success('邮件发送成功，请注意查收');
                    exit;
                }else{
                    // 将刚才插入成功的验证码记录撤销回滚
                    $EmsModel->rollback();
                    $this->error($PhpMailer->getError());
                    exit;
                }
            }else{
                $code = $this->request->param('code','','trim');
                if(empty($code)){
                    $this->error('验证码不能为空');
                    exit;
                }
    
                // 开启事务
                $EmsModel->startTrans();
                $this->BusinessModel->startTrans();
    
                // 查询这个验证码是否存在
                $where = ['email'=>$email,'code'=>$code];
                $check = $EmsModel->where($where)->find();
    
                // var_dump($check->toArray());
                // exit;
    
                // 如果没有找到记录
                if(!$check){
                    $this->error('您输入的验证码有误，请重新输入');
                    exit;
                }
    
                // 1、更新用户表
                // 2、删除验证码记录
    
                // 组装数据
                $data = [
                    'id' => $business['id'],
                    'auth' => '1'
                ];
                
                // 执行更新
                $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($data);
    
                if($BusinessStatus === FALSE){
                    $this->error('用户邮箱认证状态修改失败');
                    exit;
                }
    
                // 删除验证码记录
                $EmsStatus = $EmsModel->where($where)->delete();
    
                if($EmsStatus === FALSE){
                    // 先要将用户表的更新回滚
                    $this->BusinessModel->rollback();
                    $this->error('验证码记录删除失败');
                    exit;
                }
    
                if($BusinessStatus === FALSE || $EmsStatus === FALSE){
                    $EmsModel->rollback();
                    $this->BusinessModel->rollback();
                    $this->error('验证失败');
                    exit;
                }else{
                    // 提交事务
                    $this->BusinessModel->commit();
                    $EmsModel->commit();
                    $this->success('邮箱验证成功');
                    exit;
                }

            }           
        }
        

                
    }
}



